package com.miyu.rabbitmq.config;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
@Component
public class RabbitMQServer {
    /**
     * 推送消息队列消费处理
     * @param message
     * @param channel
     * @throws IOException
     */
    //@RabbitHandler
    //@RabbitListener(queues = "123")
    public void handleMessage(Message message, Channel channel) throws IOException {
        try {
            log.info("推送消息队列消费：handleMessage：{}",  message);
            //获取队列中的信息
            String json = new String(message.getBody());
            log.info("推送消息队列消费：handleMessage：{}",  json);

            //这段代码表示，这次消息，我已经接受并消费掉了，不会再重复发送消费
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        }catch (Exception e){

            // 处理消息失败，将消息重新放回队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true);

        }

    }

    @RabbitListener(queues = "LogQueue", concurrency = "5")
    public void hs (Message message, Channel channel) throws IOException {
        try {
            String mes = new String(message.getBody(), StandardCharsets.UTF_8);
            System.out.println(message.getMessageProperties());


//        JSONObject jsonObject = JSONObject.parseObject(mes);
            System.out.println(Thread.currentThread());
            log.info("操作序列号：{}",message.getMessageProperties().getDeliveryTag()+"");
            System.out.println(">>>>>>>>>" + mes);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }

    }
}
